<?php

namespace App\Models\Back;

use Illuminate\Database\Eloquent\Model;
use Illuminate\Support\Facades\Storage;
use Illuminate\Support\Facades\Validator;
use Illuminate\Validation\Rule;
use Intervention\Image\ImageManager;

class Brand extends Model
{
	/*
	 * 设置品牌
	 * @param   $request    object   表单数据对象
	 * return   boot    数据库更新成功返回true否则返回false
	 */
    public function set($request)
    {
	    if (isset($request->id)) {
		    $fields = self::findOrFail($request->id);
		    if (isset($request->logoPath)) {
			    $fields->logo = $request->logoPath;
		    }
	    } else {
		    $fields = $this;
		    $fields->logo = isset($request->logoPath) ? $request->logoPath : '';
	    }

	    $fields->title = $request->title;
	    $fields->site = $request->site;
	    $fields->sort_number = $request->sort_number;
	    return $fields->save();
    }

    /*
     * 添加或修改品牌表单验证
     * @param   array   $request    表单数据数组
     * return   mix     验证通过自动返回true，验证失败自动重定向到上一个页面
     */
	public function setValidator($request)
	{

		if (isset($request['id'])) {
			$id = $request['id'];
		} else {
			$id = 0;
		}

		Validator::make($request, [
			'title' => [
				'required',
				'bail',
				Rule::unique('brands')->ignore($id),
				'max:32'
			],
			'logo' => [
				'nullable',
				'image',
				'file',
				Rule::dimensions()->maxWidth(1920)->maxHeight(1920),
			],
			'site' => 'nullable|active_url',
			'sort_number' => 'integer',
			'id' => 'nullable|integer',
		])->validate();
	}

	/*
	 * 品牌logo图片上传
	 * @param   object  $request    请求的数据对象
	 * return   mix     成功返回图片设置的根目录的后的相对路径，失败返回false
	 */
	public function logoUpload($request)
	{

		//查询是否为更新，是的话就删除老logo
		if (isset($request->id)) {
			//获取
			$fields = self::findOrFail($request->id);
			//删除
			Storage::disk('logo')->delete($fields->logo);
			Storage::disk('logoThumb')->delete($fields->logo);
		}
		return $request->logo->store('', 'logo');
	}

	/*
	 * 将logo生成缩略图
	 * @param   string  $path   路径
	 */
	public function logoThumb ($name)
	{
		//获取logo的url
		$path = storage_path(config('tool.back.brand.logo.path').$name);
		$thumbPath = storage_path(config('tool.back.brand.logo.thumbPath').$name);

		//实例化图像管理类
		$manager = new ImageManager();
		$manager->make($path)->resize(50, 50)->save($thumbPath);
	}

	/*
     * 删除品牌表单验证
     * @param   array   $request    表单数据数组
     * return   mix     验证通过自动返回true，验证失败自动重定向到上一个页面
     */
	public function deleteValidator($request)
	{
		Validator::make($request, [
			'selected' => [
				'required',
				'array'
			],
			'selected.*' => [
				'integer'
			]
		])->validate();
	}

	/*
	 * 删除品牌，支持批量删除
	 * @param   array   $idList 要删除的品牌id列表
	 */
	public function deleteBrand($idList)
	{
		//获取要删除的品牌信息
		$brandList = self::find($idList);
		//遍历出有logo的品牌logo地址,删除之
		foreach ($brandList as $brand) {
			if ($brand->logo) {
				Storage::disk('logo')->delete($brand->logo);
			}
		}
		//删除数据库信息
		return self::destroy($idList);
	}

	/*
	 * 品牌名称搜索
	 * @param   object      $request      表单输入对象
	 */
	public function searchBrand($request)
	{
		//验证,此处不能用自动重定向，有可能导致get请求出先bug
		Validator::make($request, [
			'title' => [
				'required',
				'bail',
				'string',
				'max:32'
			]
		])->validate();


		//查库
		return self::where('title','like','%'.$request['title'].'%')->simplePaginate(config('tool.back.page', 5));
	}

	/*
	 * 品牌排序问题
	 */
	public function brandOrder($field, $sort)
	{
		$sort = $sort == 'asc' ? $sort : 'desc';
		return self::orderBy($field, $sort)->simplePaginate(config('tool.back.page', 5));
	}
}
